package com.ruicar.afs.cloud.afscase.backtopartnersinfo.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.backtopartnersinfo.entity.CaseBackToPartnersInfo;
import com.ruicar.afs.cloud.afscase.backtopartnersinfo.mapper.CaseBackToPartnersInfoMapper;
import com.ruicar.afs.cloud.afscase.backtopartnersinfo.service.CaseBackToPartnersInfoService;
import com.ruicar.afs.cloud.afscase.loanapprove.condition.LoanBackReasonCondition;
import com.ruicar.afs.cloud.afscase.loanapprove.entity.CaseBackReasonInfo;
import com.ruicar.afs.cloud.afscase.loanapprove.service.CaseBackReasonInfoService;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.entity.ComAttachmentManagement;
import com.ruicar.afs.cloud.image.enums.FileStatusEnum;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import com.ruicar.afs.cloud.image.service.ComAttachmentManagementService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: yk.Li
 * @date 2020-07-04 06:27:45
 * @description 退回合作商接口信息表 service实现类
 */
@Service
@AllArgsConstructor
@Slf4j
public class CaseBackToPartnersInfoServiceImpl extends ServiceImpl<CaseBackToPartnersInfoMapper, CaseBackToPartnersInfo> implements CaseBackToPartnersInfoService {

    private CaseBackReasonInfoService caseBackReasonInfoService;

    private ComAttachmentManagementService comAttachmentManagementService;

    private ComAttachmentFileService comAttachmentFileService;
    @Override
    @Transactional(rollbackFor=Exception.class,propagation = Propagation.REQUIRED)
    public void saveBackPartner(LoanBackReasonCondition loanBackReasonCondition) {
        CaseBackToPartnersInfo caseBackToPartnersInfo = new CaseBackToPartnersInfo();
        caseBackToPartnersInfo.setBackWords(loanBackReasonCondition.getBackWords());
        caseBackToPartnersInfo.setFileType(loanBackReasonCondition.getFileType());
        caseBackToPartnersInfo.setApplyNo(loanBackReasonCondition.getApplyNo());
        caseBackToPartnersInfo.setContractNo(loanBackReasonCondition.getContractNo());
        caseBackToPartnersInfo.setTaskId(loanBackReasonCondition.getTaskId());
        caseBackToPartnersInfo.setBackDesc(loanBackReasonCondition.getBackDesc());
        List<CaseBackReasonInfo> caseBackReasonInfo = caseBackReasonInfoService.list(Wrappers.<CaseBackReasonInfo>query().lambda()
                .eq(CaseBackReasonInfo::getAttachmentId,loanBackReasonCondition.getFileType()));
        caseBackToPartnersInfo.setFileTypeName(caseBackReasonInfo.get(0).getAttachmentName());
        this.save(caseBackToPartnersInfo);
        List fileTypeList = new ArrayList();
        fileTypeList.add(loanBackReasonCondition.getFileType());
        List<String> parentIdlist = comAttachmentManagementService.list(Wrappers.<ComAttachmentManagement>query().lambda()
                .in(ComAttachmentManagement::getParentId,fileTypeList))
                .stream()
                .map(m -> String.valueOf(m.getId())).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(parentIdlist)){
            List<ComAttachmentFile> attachmentFiles = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                    .eq(ComAttachmentFile::getBusiNo,loanBackReasonCondition.getContractNo())
                    .eq(ComAttachmentFile::getHistoryVersion,"0")
                    .in(ComAttachmentFile::getAttachmentCode,parentIdlist));
            if(CollectionUtils.isNotEmpty(attachmentFiles)){
                attachmentFiles.forEach(attachmentFile->{
                    attachmentFile.setFileStatus(FileStatusEnum.NOTSTANDARD.getCode());
                    comAttachmentFileService.updateById(attachmentFile);
                });
            }
        }
        List<ComAttachmentFile> attachmentFileList = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                .eq(ComAttachmentFile::getAttachmentCode,loanBackReasonCondition.getFileType())
                .eq(ComAttachmentFile::getHistoryVersion,"0")
                .eq(ComAttachmentFile::getBusiNo,loanBackReasonCondition.getContractNo()));
        if(attachmentFileList != null && attachmentFileList.size() != 0){
            attachmentFileList.forEach(comAttachmentFile -> {
                comAttachmentFile.setFileStatus(FileStatusEnum.NOTSTANDARD.getCode());
                comAttachmentFileService.updateById(comAttachmentFile);
            });
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class,propagation = Propagation.REQUIRED)
    public void editBackPartner(LoanBackReasonCondition loanBackReasonCondition) {
        CaseBackToPartnersInfo caseBackToPartnersInfo = this.getOne(Wrappers.<CaseBackToPartnersInfo>query().lambda()
                .eq(CaseBackToPartnersInfo::getId,loanBackReasonCondition.getId()));
        List<ComAttachmentFile> oldAttachmentFileList = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                .eq(ComAttachmentFile::getAttachmentCode,caseBackToPartnersInfo.getFileType())
                .eq(ComAttachmentFile::getHistoryVersion,"0")
                .eq(ComAttachmentFile::getBusiNo,loanBackReasonCondition.getContractNo()));
        List oldfileTypeList = new ArrayList();
        oldfileTypeList.add(caseBackToPartnersInfo.getFileType());
        List<String> oldParentIdlist = comAttachmentManagementService.list(Wrappers.<ComAttachmentManagement>query().lambda()
                .in(ComAttachmentManagement::getParentId,oldfileTypeList))
                .stream()
                .map(m -> String.valueOf(m.getId())).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(oldParentIdlist)){
            List<ComAttachmentFile> oldAttachmentFiles = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                    .eq(ComAttachmentFile::getBusiNo,loanBackReasonCondition.getContractNo())
                    .eq(ComAttachmentFile::getHistoryVersion,"0")
                    .in(ComAttachmentFile::getAttachmentCode,oldParentIdlist));
            if(CollectionUtils.isNotEmpty(oldAttachmentFiles)){
                oldAttachmentFiles.forEach(oldAttachmentFile->{
                    oldAttachmentFile.setFileStatus(FileStatusEnum.STANDARD.getCode());
                    comAttachmentFileService.updateById(oldAttachmentFile);
                });
            }
        }
        if(oldAttachmentFileList != null && oldAttachmentFileList.size() != 0){
            oldAttachmentFileList.forEach(oldAttachmentFile -> {
                oldAttachmentFile.setFileStatus(FileStatusEnum.STANDARD.getCode());
                comAttachmentFileService.updateById(oldAttachmentFile);
            });
        }
        caseBackToPartnersInfo.setBackWords(loanBackReasonCondition.getBackWords());
        caseBackToPartnersInfo.setBackDesc(loanBackReasonCondition.getBackDesc());
        caseBackToPartnersInfo.setFileType(loanBackReasonCondition.getFileType());
        List<CaseBackReasonInfo> caseBackReasonInfo = caseBackReasonInfoService.list(Wrappers.<CaseBackReasonInfo>query().lambda()
                .eq(CaseBackReasonInfo::getAttachmentId,loanBackReasonCondition.getFileType()));
        caseBackToPartnersInfo.setFileTypeName(caseBackReasonInfo.get(0).getAttachmentName());
        this.updateById(caseBackToPartnersInfo);
        List fileTypeList = new ArrayList();
        fileTypeList.add(loanBackReasonCondition.getFileType());
        List<String> parentIdlist = comAttachmentManagementService.list(Wrappers.<ComAttachmentManagement>query().lambda()
                .in(ComAttachmentManagement::getParentId,fileTypeList))
                .stream()
                .map(m -> String.valueOf(m.getId())).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(parentIdlist)){
            List<ComAttachmentFile> attachmentFiles = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                    .eq(ComAttachmentFile::getBusiNo,loanBackReasonCondition.getContractNo())
                    .eq(ComAttachmentFile::getHistoryVersion,"0")
                    .in(ComAttachmentFile::getAttachmentCode,parentIdlist));
            if(CollectionUtils.isNotEmpty(attachmentFiles)){
                attachmentFiles.forEach(attachmentFile->{
                    attachmentFile.setFileStatus(FileStatusEnum.NOTSTANDARD.getCode());
                    comAttachmentFileService.updateById(attachmentFile);
                });
            }
        }
        List<ComAttachmentFile> attachmentFileList = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                .eq(ComAttachmentFile::getAttachmentCode,loanBackReasonCondition.getFileType())
                .eq(ComAttachmentFile::getHistoryVersion,"0")
                .eq(ComAttachmentFile::getBusiNo,loanBackReasonCondition.getContractNo()));
        if(attachmentFileList != null && attachmentFileList.size() != 0){
            attachmentFileList.forEach(comAttachmentFile -> {
                comAttachmentFile.setFileStatus(FileStatusEnum.NOTSTANDARD.getCode());
                comAttachmentFileService.updateById(comAttachmentFile);
            });
        }
    }

    @Override
    public void deleteBackPartner(LoanBackReasonCondition loanBackReasonCondition) {
        CaseBackToPartnersInfo caseBackToPartnersInfo = this.getOne(Wrappers.<CaseBackToPartnersInfo>query().lambda()
                .eq(CaseBackToPartnersInfo::getId,loanBackReasonCondition.getId()));
        this.removeById(caseBackToPartnersInfo.getId());
        List fileTypeList = new ArrayList();
        fileTypeList.add(loanBackReasonCondition.getFileType());
        List<String> parentIdlist = comAttachmentManagementService.list(Wrappers.<ComAttachmentManagement>query().lambda()
                .in(ComAttachmentManagement::getParentId,fileTypeList))
                .stream()
                .map(m -> String.valueOf(m.getId())).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(parentIdlist)){
            List<ComAttachmentFile> attachmentFiles = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                    .eq(ComAttachmentFile::getBusiNo,loanBackReasonCondition.getContractNo())
                    .eq(ComAttachmentFile::getHistoryVersion,"0")
                    .in(ComAttachmentFile::getAttachmentCode,parentIdlist));
            if(CollectionUtils.isNotEmpty(attachmentFiles)){
                attachmentFiles.forEach(attachmentFile->{
                    attachmentFile.setFileStatus(FileStatusEnum.STANDARD.getCode());
                    comAttachmentFileService.updateById(attachmentFile);
                });
            }
        }
        List<ComAttachmentFile> attachmentFileList = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                .eq(ComAttachmentFile::getAttachmentCode,loanBackReasonCondition.getFileType())
                .eq(ComAttachmentFile::getHistoryVersion,"0")
                .eq(ComAttachmentFile::getBusiNo,loanBackReasonCondition.getContractNo()));
        if(attachmentFileList != null && attachmentFileList.size() != 0){
            attachmentFileList.forEach(comAttachmentFile -> {
                comAttachmentFile.setFileStatus(FileStatusEnum.STANDARD.getCode());
                comAttachmentFileService.updateById(comAttachmentFile);
            });
        }
    }
}
